package com.gdut.sport.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.gdut.sport.R;
import com.gdut.sport.db.History;

import java.util.List;

public class HistoryListAdapter extends BaseAdapter {

    private List<History> list;
    private LayoutInflater inflater;

    public HistoryListAdapter(Context context, List<History> list) {
        this.list = list;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public int getCount() {
        return list != null ? list.size() : 0;
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.layout_history_item, parent, false);
            holder = new ViewHolder();
            holder.content = (TextView) convertView.findViewById(R.id.id_content_text);
            holder.time = (TextView) convertView.findViewById(R.id.id_time_text);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        holder.content.setText("路程：" + list.get(position).getDistance() + "m\t" + "耗时：" + list.get(position).getTime());
        holder.time.setText(list.get(position).getCreatedAt());
        return convertView;
    }

    private static class ViewHolder {
        TextView content;
        TextView time;
    }
}
